package net.fonline.apps;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;

/**
 * This class contains all general game-information.
 * The command central. Because everything is bound 
 * to an output and this class manages the output.
 * @author Michael
 */
public class EchoCanvas extends Canvas {

    private int lastX;
    private int lastY = 0;
    Display display;
    OnClickEvents event_handler;
    public int coins = 0;
    public MapManager map;
    public Cheatbot cheatbot;
    public MissileList missiles;
    private boolean dragged = false;
    public int relX;
    public int relY;
    public boolean popup;
    public double scale;
    private int dx;
    private int dy;
    public int lives;

    public EchoCanvas(Display d) {
        this.display = d;
        setFullScreenMode(true);
        this.event_handler = new OnClickEvents(this);
        this.map = new MapManager(this);
        this.missiles = new MissileList();
        this.scale = (0.008D * getWidth());
        this.dx = 0;
        this.dy = 0;
        this.relX = 0;
        this.relY = 0;
        this.popup = false;
        this.cheatbot = new Cheatbot();
        this.lives = 100;
    }

    public void paint(Graphics g) {
        g.setGrayScale(255);
        g.setColor(100, 80, 50);
        g.fillRect(0, 0, getWidth(), getHeight());

        this.map.update(g);
        this.cheatbot.update(g);
        this.missiles.update(g);

        g.setStrokeStyle(0);
        g.setGrayScale(0);
        g.drawArc(this.lastX - 2, this.lastY - 2, 4, 4, 0, -360);

        g.drawString("Lives: " + lives+" | Coins: "+coins, 2, getHeight() - 22, 20);
//        g.drawString(this.action + " At: (" + (int) ((this.lastX - this.relX) / this.scale) + ", " + (int) ((this.lastY - this.relY) / this.scale) + ") Scale: " + this.scale, 0, getHeight() - 20, 20);
    }

    protected void pointerPressed(int x, int y) {
        this.dragged = false;
        this.dx = x;
        this.dy = y;
        setCoords(x, y, "Pressed");
    }

    protected void pointerReleased(int x, int y) {
        if (this.dragged) {
            this.dragged = false;
            setCoords(x, y, "Released");
        } else {
            this.event_handler.update(x, y);
            setCoords(x, y, "Event");
        }
    }

    protected void pointerDragged(int x, int y) {
        this.dragged = true;
        this.relX += x - this.dx;
        this.relY += y - this.dy;
        this.dx = x;
        this.dy = y;
        setCoords(x, y, "Dragged");
    }

    private void setCoords(int x, int y, String action) {
        this.lastX = x;
        this.lastY = y;
    }
}